package com.yanqu.road.server.pb;

import com.yanqu.road.entity.grandchild.GrandChildData;
import com.yanqu.road.entity.grandchild.GrandChildOfficial;
import com.yanqu.road.entity.grandchild.GrandChildProfession;
import com.yanqu.road.entity.grandchild.SchoolSiteData;
import com.yanqu.road.entity.marriage.MarriageRecord;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.grandchild.GrandChildProto;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class GrandChildPb {
    public static GrandChildProto.MarriageRecordMsg.Builder parseMarriageRecordTempMsg(MarriageRecord marriageRecord) {
        if (null == marriageRecord) {
            return null;
        }
        GrandChildProto.MarriageRecordMsg.Builder msg = GrandChildProto.MarriageRecordMsg.newBuilder();
        msg.setChildUid(marriageRecord.getChildUid());
        msg.setChildTalent(marriageRecord.getChildTalent());
        msg.setChildOccupation(marriageRecord.getChildOccupation());
        msg.setChildName(marriageRecord.getChildName());
        msg.setChildSex(marriageRecord.getChildSex());
        msg.setChildHeadIcon(marriageRecord.getChildHeadIcon());
        msg.setPartnerUserId(marriageRecord.getPartnerUserId());
        msg.setPartnerNickName(marriageRecord.getPartnerNickName());
        msg.setPartnerChildTalent(marriageRecord.getPartnerChildTalent());
        msg.setPartnerChildSex(marriageRecord.getPartnerChildSex());
        msg.setPartnerChildOccupation(marriageRecord.getPartnerChildOccupation());
        msg.setPartnerChildName(marriageRecord.getPartnerChildName());
        msg.setPartnerChildHeadIcon(marriageRecord.getPartnerChildHeadIcon());
        msg.setMarriageTime(marriageRecord.getMarriageTime());
        msg.setChildOccupationId(marriageRecord.getChildOccupationId());
        msg.setChildEarnSpeed(marriageRecord.getChildEarnSpeed().toString());
        msg.setPartnerChildOccupationId(marriageRecord.getPartnerChildOccupationId());
        msg.setPartnerChildEarnSpeed(marriageRecord.getPartnerChildEarnSpeed().toString());
        msg.setTotalChildEarnSpeed(marriageRecord.getTotalChildEarnSpeed().toString());
        msg.setMotherId(marriageRecord.getMotherId());
        msg.setPartnerMotherId(marriageRecord.getPartnerMotherId());
        msg.setChildIsTwins(marriageRecord.isTwins());
        msg.setPartnerIsTwins(marriageRecord.isPartnerTwins());
        msg.setServerId(marriageRecord.getServerId());
        msg.setPartnerServerId(marriageRecord.getPartnerServerId());
        return msg;
    }

    public static GrandChildProto.GrandChildMsg.Builder parseGrandChildMsg(GrandChildData data) {
        GrandChildProto.GrandChildMsg.Builder msg = GrandChildProto.GrandChildMsg.newBuilder();
        msg.setId(data.getId());
        msg.setAppearanceId(data.getAppearanceId());
        msg.setName(data.getNickName());
        msg.setLv(data.getLv());
        msg.setOccupation(data.getOccupation());
        msg.setBaseQuality(data.getBaseQuality());
        for (Integer flairId : data.getFlairList()) {
            msg.addFlair(flairId);
        }
        for (Integer talentId : data.getTalentList()) {
            msg.addTalent(talentId);
        }
        for (Integer optionalId : data.getOptionalList()) {
            msg.addOptional(optionalId);
        }
        msg.setReTalentTimes(data.getReTalentTimes());
        msg.setArchaeus(data.getArchaeus());
        if (data.getArchaeus() >= GrandChildConfigMgr.getMaxArchaeus()) {
            //满了
            msg.setArchaeusRecoverTime(0);
        }else {
            int nextTime = DateHelper.toSecond(data.getArchaeusLastRecoverTime() + GameConfig.GRAND_CHILD_ARCHAEUS_RECOVER_TIME * 60 * 1000);
            msg.setArchaeusRecoverTime(nextTime);
        }
        msg.setEarnSpeed(data.getEarnSpeed().toString());
        msg.setSex(data.getSex());
        msg.setQuality(data.getQuality());
        msg.setMaxLv(data.getMaxLv());
        if (data.getPatronsId() > 0) {
            msg.setPatronsId(data.getPatronsId());
        }
        msg.setHadExam(data.getHadExam() == 1);
        if (data.getProfessionId() > 0) {
            msg.setProfessionId(data.getProfessionId());
        }
        if (data.getAffinityPatronsId() > 0) {
            msg.setAffinityPartonsId(data.getAffinityPatronsId());
        }
        msg.setTitle(data.getTitle());
        return msg;
    }

    public static GrandChildProto.SchoolSiteMsg.Builder parseSchoolSiteMsg(SchoolSiteData data, GrandChildData child) {
        GrandChildProto.SchoolSiteMsg.Builder msg = GrandChildProto.SchoolSiteMsg.newBuilder();
        msg.setType(data.getSchoolType());
        msg.setPos(data.getPos());
        if (child != null) {
            GrandChildProto.GrandChildMsg.Builder childMsg = parseGrandChildMsg(child);
            msg.setGrandChild(childMsg);
        }
        return msg;
    }

    public static GrandChildProto.PupilDataMsg.Builder parsePupilShowData(GrandChildData data) {
        GrandChildProto.PupilDataMsg.Builder msg = GrandChildProto.PupilDataMsg.newBuilder();
        msg.setAppearanceId(data.getAppearanceId());
        msg.setEarnSpeed(data.getEarnSpeed().toString());
        msg.setId(data.getId());
        msg.setTitle(data.getTitle());
        return msg;
    }

    public static GrandChildProto.FamilyTreeProfessionMsg.Builder parseFamilyTreeProfessionMsg(GrandChildProfession data, int appearanceId, BigInteger earnSpeed) {
        if (null == data) {
            return null;
        }
        GrandChildProto.FamilyTreeProfessionMsg.Builder msg = GrandChildProto.FamilyTreeProfessionMsg.newBuilder();
        msg.setProfessionId(data.getProfessionId());
        msg.setCount(data.getGrandChildCount());
        msg.setSkillLv(data.getSkillLv());
        msg.setAppearanceId(appearanceId);
        msg.setEarnSpeed(earnSpeed.toString());
        return msg;
    }

    public static GrandChildProto.FamilyTreeProfessionListMsg.Builder parseFamilyTreeProfessionListMsg(List<GrandChildProfession> dataList,
                                                                                                       Map<Integer, GrandChildData> professionTopMap) {
        GrandChildProto.FamilyTreeProfessionListMsg.Builder listMsg = GrandChildProto.FamilyTreeProfessionListMsg.newBuilder();
        if (null != dataList) {
            for (GrandChildProfession data : dataList) {
                int appearanceId = 0;
                BigInteger earnSpeed = BigInteger.ZERO;
                GrandChildData grandChildData = professionTopMap.get(data.getProfessionId());
                if(null != grandChildData){
                    appearanceId = grandChildData.getAppearanceId();
                    earnSpeed = grandChildData.getEarnSpeed();
                }
                GrandChildProto.FamilyTreeProfessionMsg.Builder msg = parseFamilyTreeProfessionMsg(data, appearanceId, earnSpeed);
                if (null != msg) {
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static GrandChildProto.FamilyTreeOfficialMsg.Builder parseFamilyTreeOfficialMsg(GrandChildOfficial data) {
        if (null == data) {
            return null;
        }
        GrandChildProto.FamilyTreeOfficialMsg.Builder msg = GrandChildProto.FamilyTreeOfficialMsg.newBuilder();
        msg.setOfficialId(data.getOfficialId());
        msg.setCount(data.getGrandChildCount());
        return msg;
    }

    public static GrandChildProto.FamilyTreeOfficialListMsg.Builder parseFamilyTreeOfficialListMsg(List<GrandChildOfficial> dataList) {
        GrandChildProto.FamilyTreeOfficialListMsg.Builder listMsg = GrandChildProto.FamilyTreeOfficialListMsg.newBuilder();
        if (null != dataList) {
            for (GrandChildOfficial data : dataList) {
                GrandChildProto.FamilyTreeOfficialMsg.Builder msg = parseFamilyTreeOfficialMsg(data);
                if (null != msg) {
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }
}
